#!/bin/bash
# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: jaewon

# To invoke fastfeed ops with slurm:
# 1. Login as bot-coin.
# 2. Go to /remote/iosg/coin/deploy/coin_fastfeed/coin.
# 3. Activate conda fastfeed env.
# 4. sbatch -n64 --priority=TOP python/coin/feed/fastfeed/ops/gen_feed_cache_slurm.sbatch -2

set -Eeo pipefail
export TZ=UTC
date

if [[ -z "${SLURM_JOBID}" || -z "${SLURM_NTASKS}" ]]; then
  (>&2 echo "This script must be invoked using sbatch -n ...!")
  exit 1
fi

# Env
REMOTE_REPO_DIR="/remote/iosg/coin/deploy/coin_fastfeed"
cd "${REMOTE_REPO_DIR}"
source "miniconda3/etc/profile.d/conda.sh"
conda activate fastfeed
cd "${REMOTE_REPO_DIR}/coin/python"

# Config
FEED_CACHE_DIR="/remote/iosg/coin-2/buckets/feed.derived.feed_cache.coin/"
RUNFILE_DIR="${REMOTE_REPO_DIR}/run"
DATE="${1}"
DRY_RUN="${2}"
NUM_PARALLEL="${SLURM_NTASKS}"

if [ -z "${DATE}" ]; then
  (>&2 echo "Usage: ${0} <YYYYMMDD> <dry_run?>")
  exit 1
fi

if [ "${DATE}" == "today" ]; then
  DATE="$(date '+%Y%m%d')"
elif [ "${DATE}" == "-1" ]; then
  DATE="$(date --date=yesterday '+%Y%m%d')"
elif [ "${DATE}" == "-2" ]; then
  DATE="$(date --date='2 days ago' '+%Y%m%d')"
elif [ "${DATE}" == "-3" ]; then
  DATE="$(date --date='3 days ago' '+%Y%m%d')"
fi

RUNFILE="${RUNFILE_DIR}/run_${DATE}.sh"

echo "DATE=${DATE}"
echo "NUM_PARALLEL=${NUM_PARALLEL}"
echo "RUNFILE=${RUNFILE}"

export USE_DYNAMIC_SYMBOL=1

mkdir -p "${RUNFILE_DIR}"
./pyrunner coin/feed/fastfeed/ops/gen_feed_conv_cmd.py \
  --date="${DATE}" \
  --version="3" \
  --feed_cache_dir="${FEED_CACHE_DIR}" > ${RUNFILE}

if [ -n "${DRY_RUN}" ]; then
  (>&2 echo "Dry-run")
  exit 0
fi

set +eE
time parallel -j "$((NUM_PARALLEL+2))" < ${RUNFILE}
RET="${?}"

# Print results
grep "^${DATE}" status | sort
exit ${?}
